/**
 * 
 */
package network;

import io.ImportGame;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import game.Building;
import game.Game;
import game.Player;
import game.Unit;
import game.UnitType;

/**
 * @author jb
 * 
 * Modified by Stefan
 *
 */
// TODO: GameClient class
public class GameClient extends GameNetwork {
	
	@SuppressWarnings("unused")
	private Socket client;
	private BufferedReader cmdIStream;
	private DataOutputStream cmdOStream;
	
	public GameClient(Socket client) throws IOException
	{
		super(null, null, null);
		
		this.client = client;
		this.cmdIStream= new BufferedReader((new InputStreamReader(client.getInputStream())), 1);
		this.cmdOStream = new DataOutputStream(client.getOutputStream());

	}
	
	
	//
	// Low level commands
	//
	
	public void transfertGameFile() throws IOException {
		ImportGame importGame = new ImportGame(cmdIStream);
		importGame.read(this);
		this.setStarted(true);
	}
	
	
	public void readSocketClients()
	{
		try 
		{
			this.parseCommand(this.cmdIStream.readLine());
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	public void setTile(boolean server, int x, int y, int tile)
	{
		if (server == false)
		{
			String command = "setTile\t"+ x + "\t" + y + "\t" + tile;
			
			try 
			{
				this.cmdOStream.writeBytes(command + "\n");
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		super.setTile(server, x, y, tile);
		
		
	}
	
	public void placeUnit(boolean server, Unit unit) 
	{
		if (server == false)
		{
			String command = "placeUnit\t" + unit;
			
			try 
			{
				this.cmdOStream.writeBytes(command + "\n");
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
			
		}
		super.placeUnit(server, unit);
	}
	
	public void removeUnit(boolean server, Unit unit) 
	{
		if (server == false)
		{
			String command = "removeUnit\t" + unit;
			
			try 
			{
				this.cmdOStream.writeBytes(command + "\n");
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		super.removeUnit(server, unit);
	}
	
	public void updateUnit(boolean server, Unit unit, Unit newProperties)
	{
		if (server == false)
		{
			String command = "updateUnit\t" + unit + "\t" + newProperties;
			
			try 
			{
				this.cmdOStream.writeBytes(command + "\n");
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		super.updateUnit(server, unit, newProperties);
	}
	
	public void placeBuilding(boolean server, Building building) 
	{
		if (server == false)
		{
			String command = "placeBuilding\t" + building;
			
			try 
			{
				this.cmdOStream.writeBytes(command + "\n");
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		super.placeBuilding(server, building);
	}
	
	public void removeBuilding(boolean server, Building building) 
	{
		if (server == false)
		{
			String command = "removeBuilding\t" + building;
			
			try 
			{
				this.cmdOStream.writeBytes(command + "\n");
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		super.removeBuilding(server, building);
	}
	
	public void updateBuilding(boolean server, Building building, Building newProperties) 
	{
		if (server == false)
		{
			String command = "updateBuilding\t" + building + "\t" + newProperties;
			
			try 
			{
				this.cmdOStream.writeBytes(command + "\n");
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		
		super.updateBuilding(server, building, newProperties);
	}
	
	public void playerLost(boolean server,Player player) 
	{
		if (server == false)
		{
			String command = "playerLost\t" + player;
			
			try 
			{
				this.cmdOStream.writeBytes(command + "\n");
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		super.playerLost(server, player);
	}
	
	public void endTurn(boolean server)
	{
		if (server == false)
		{
			String command = "endTurn";
			
			try 
			{
				this.cmdOStream.writeBytes(command + "\n");
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		super.endTurn(server);
	}
	
	/**
	 * Pump commands to synchronize game with server
	 * @throws IOException 
	 */
	public void pumpCommmands() throws IOException {
		while(this.cmdIStream.ready()) {
			String command = this.cmdIStream.readLine();
			this.parseCommand(command);
		}
	}
	
	public static void main (String [] a) throws UnknownHostException, IOException {
		Socket s = new Socket(InetAddress.getByName("localhost"), 34123);
		GameClient client = new GameClient(s);
		
		while(true) {
			client.pumpCommmands();
		}
	}
}
